//app.js
App({
  data: {
    baseURL: "https://min.lexiangpingou.cn/minapi.php?uniacid=33&app=0&",
    newURL: "https://min.lexiangpingou.cn/min_shop_api.php?uniacid=33&app=0&",
    lastURL: "https://min.lexiangpingou.cn/min_cart_api.php?uniacid=33&app=0&",
    testURL: "https://min.lexiangpingou.cn/Base64code/decode.php?uniacid=33&app=0&",
    XCX_PAY: "https://min.lexiangpingou.cn/addons/lexiangpingou/lxapi/xcx_pay.php?uniacid=33&app=0&",
    jielong: "https://min.lexiangpingou.cn/applet/controller.php?",
    jlpay: 'https://min.lexiangpingou.cn/applet/applet.php?',
    jlway: 'https://min.lexiangpingou.cn/applet/active.php?',
    jecheck: 'https://min.lexiangpingou.cn/communityapi/customer.php?',
    GZHid: "33",
    unionId: '',
    IP: "",
    openid: "",
    session_key: "",
    comName: "火蝶云",
    userInfo: {},
    mid: '', //转发获取到的兼职人员id
    parentMid: '', //兼职人员id
    shareImage: '', //兼职人员转发背景图
    appid: "wxd11509514ffc5faf",
    secret: "6cf43797404c025f08e74ca4b1dfd6ce",
    body: "火蝶云-", //商品描述，支付接口使用，样例：罗辑思维-图书
    paykey: "zl5dscvpfbemgzf3jddw3wgpeorjqsky", //密钥，支付使用
  },
  onLaunch: function(options) {
    console.log('APP:' + JSON.stringify(options))
    console.log(JSON.stringify(options).query)
    this.data.mid = options.query.mid;
    var that = this;
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    that.getUserInfo()
  },
  getUserInfo: function(cb) {
    var that = this;
    if (that.data.openid != "") {
      return false;
    }
    //调用登录接口
    wx.login({
      success: function(res) {
        wx.request({
          url: that.data.baseURL + 'op=wx_login',
          data: {
            appid: that.data.appid,
            secret: that.data.secret,
            code: res.code,
            grant_type: "authorization_code"
          },
          success: function(res) {
            console.log(res)
            that.data.openid = res.data.openid;
            wx.setStorageSync('openid', res.data.openid)
            that.data.session_key = res.data.session_key;
            // console.log(res);
            that.requestUserInfo((mid) => {
              that.requestGetShareImage((shareImage) => {
                that.data.parentMid = mid;
                that.data.shareImage = shareImage.bg;
              })
            })
            that.wxGetUserInfo(res.data.session_key, res.data.openid);
            wx.setStorage({
              key: "openid",
              data: res.data.openid
            })
            wx.setStorage({
              key: "session_key",
              data: res.data.session_key
            })
            wx.request({
              url: that.data.baseURL + 'op=test',
              data: {
                openid: res.data.openid
              },
              success: function(res) {
                wx.setStorage({
                  key: "IP",
                  data: res.data.ip
                })
                that.data.IP = res.data.ip;
              }
            })
          },
          fail: function(res) {}
        })
      }
    });
  },
  requestUserInfo(success) { //获取申请人信息
    wx.request({
      url: this.data.XCX_PAY + '&op=getUserInfo',
      data: {
        openid: this.data.openid
      },
      success: function(res) {
        console.log(res.data)
        if (res.data.status) {
          success && success(res.data.msg.id)
        }
      }
    })
  },
  requestGetShareImage(success) {
    wx.request({
      url: this.data.XCX_PAY + '&op=getshareimage',
      data: {
        openid: this.data.openid
      },
      success: function(res) {
        success && success(res.data)
      }
    })
  },
  globalData: {
    userInfo: null
  },
  wxGetUserInfo(session_key, openid) {
    var that = this;
    wx.getUserInfo({
      success: function(res) {
        that.globalData.userInfo = res.userInfo;
        that.data.userInfo = res.userInfo;
        typeof cb == "function" && cb(that.globalData.userInfo);
        wx.request({
          url: that.data.baseURL + 'op=check_member',
          data: {
            appid: that.data.appid,
            encryptedData: res.encryptedData,
            iv: res.iv,
            session_key: session_key,
            openid: openid,
            nickname: res.userInfo.nickName,
            avatar: res.userInfo.avatarUrl,
            gender: res.userInfo.gender,
            province: res.userInfo.province,
            city: res.userInfo.city,
            gender: res.userInfo.gender,
            mid: that.data.mid ? that.data.mid : '',
            xcx: 1
          },
          success: function(res) {
            console.log(res);

            if (res.data.info.unionId) {
              wx.setStorage({
                key: "unionId",
                data: res.data.info.unionId
              })
              that.data.unionId = res.data.info.unionId;
            }
            var data = res.data.member;
            if (data.addmobile && data.addname) {
              wx.setStorage({
                key: "add",
                data: {
                  addmobile: data.addmobile,
                  addname: data.addname
                }
              })
            }
          }
        })
      }
    })
  }
})
